/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import entities.Author;
import entities.Book;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;


@ManagedBean(name="bookHelper")
@RequestScoped
public class BookHelper {
    private long id;
    private String title;
    private String description;
    private Double price;
    //private List<Author> authors;
    private Author[] authors;
    //
    private List<String> authorsId;
    public BookHelper()
    {
        //authors = new ArrayList<Author>();
        authors = new AuthorDAO().getAllAuthors().toArray(new Author[0]);
        authorsId = new ArrayList<String>();

    }
    public BookHelper(String title, String description, Double price) {
        this.title = title;
        this.description = description;
        this.price = price;
        //authors = new ArrayList<Author>();
        authors = new AuthorDAO().getAllAuthors().toArray(new Author[0]);
        authorsId = new ArrayList<String>();
    }

    public BookHelper(String title, String description, Double price, Author[] authors) {
        this.title = title;
        this.description = description;
        this.price = price;
        this.authors = authors;
    }

    public BookHelper(Book book)
    {
        title = book.getTitle();
        description = book.getDescription();
        price = book.getPrice();
        authors = new AuthorDAO().getAllAuthors().toArray(new Author[0]);
        authorsId = new ArrayList<String>();
        for(Author author: book.getAuthors())
            authorsId.add(String.valueOf(author.getId()));
        id = book.getId();
        System.out.println("tworze obiekt bookHelper z id "+id);
        //setAuthors(book.getAuthors());
    }
    public Author[] getAuthors() {
        return authors;
    }

    public void setAuthors(Author[] authors) {
        this.authors = authors;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<String> getAuthorsId() {
        return authorsId;
    }

    public void setAuthorsId(List<String> authorsId) {
        this.authorsId = authorsId;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }


    

    public String create() {
        BookDAO bookDao = new BookDAO();
        AuthorDAO authorDao = new AuthorDAO();
        Book book = new Book(title, price, description);
        for (String s : authorsId) {
            long authorId = extractId(s);
            Author a =  authorDao.findAuthor(authorId);
            book.getAuthors().add(a);
        }
        //SessionManager sm = new SessionManager();

        SessionManager.getInstance().setRequestItem("messageBean", new MessageBean("Dodano nową książkę"));
        bookDao.create(book);
        return "admin";
    }

    public String update()
    {
        System.out.println("wywoluje update na BookHelper z id="+id +  " i title = "+title);
        BookDAO bookDAO = new BookDAO();
        AuthorDAO authorDao = new AuthorDAO();
        Book book = bookDAO.findBook(id);
        List<Author> bookAuthors = new ArrayList<Author>();
        for (String s : authorsId) {
            long authorId = extractId(s);
            Author a =  authorDao.findAuthor(authorId);
            bookAuthors.add(a);
        }
        System.out.println("tytul = "+book.getTitle() + ", id="+book.getId());
        
        try{
            book.setAuthors(bookAuthors);
            book.setTitle(title);
            book.setDescription(description);
            book.update();
        } catch(Exception e) {
            System.out.println("BLAD PODCZAS UPDATOWANIA KSIAZKI, "+e.getMessage());
        }
        return "admin";
    }

    private long extractId(String s) {
        int start = s.indexOf("id")+3;
        int end = s.indexOf("]");
        String id = s.substring(start, end);
        return Long.parseLong(id);
    }

}
